﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using JQueryControlExtenders.BaseExtender;

namespace JQueryControlExtenders.Extenders
{
    [TargetControlType(typeof(Panel))]
    public class PanelDialogExtender : ExtenderControl
    {
        /// <summary>
        /// When autoOpen is true the dialog will open automatically when dialog is called. If false it will stay hidden until .dialog("open") is called on it.
        /// </summary>
        public bool AutoOpen { get; set; }
        /// <summary>
        /// The height of the dialog, in pixels.
        /// </summary>
        public int Height { get; set; }
        /// <summary>
        /// The width of the dialog, in pixels.
        /// </summary>
        public int Width { get; set; }
        /// <summary>
        /// If set to true, the dialog will have modal behavior; other items on the page will be disabled (i.e. cannot be interacted with). Modal dialogs create an overlay below the dialog but above other page elements.
        /// </summary>
        public bool Modal { get; set; }
        /// <summary>
        /// If set to true, the dialog will be draggable will be draggable by the titlebar.
        /// </summary>
        public bool Draggable { get; set; }
        /// <summary>
        /// Specifies the title of the dialog. The title can also be specified by the title attribute on the dialog source element.
        /// </summary>
        public string Title { get; set; }
        /// <summary>
        /// If set to true, the dialog will be resizeable.
        /// </summary>
        public bool Resizable { get; set; }

        /// <summary>
        /// Re-appends the dialog back inside the 'form' tag so the update panel posted back
        /// </summary>
        public bool EnableUpdatePanelCompatability { get; set; }

        /// <summary>
        /// Specifies which buttons should be displayed on the dialog. 
        /// The property key is the text of the button. 
        /// The value is the callback function for when the button is clicked. 
        /// The context of the callback is the dialog element; 
        /// if you need access to the button, it is available as the target of the event object.
        /// </summary>
        /// <example>
        /// <code>
        /// $('.selector').dialog({ buttons: { "Ok": function() { $(this).dialog("close"); } } });
        /// </code>
        /// </example>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public string Buttons { get; set; }

        /// <summary>
        /// This event is triggered when the dialog is closed.
        /// </summary>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public string ClientEventClose { get; set; }

        private string _ClientEventOpen;
        /// <summary>
        /// This event is triggered when dialog is opened.
        /// </summary>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public string ClientEventOpen
        {
            get
            {
                if (EnableUpdatePanelCompatability)
                { //based on fix at: http://blog.roonga.com.au/2009/07/using-jquery-ui-dialog-with-aspnet-and.html
                    return "function(type, data) { $(this).parent().appendTo(\"form\"); }";
                }
                return _ClientEventOpen;
            }
            set
            {
                if (EnableUpdatePanelCompatability)
                    throw new NotSupportedException(string.Format("ClientEventOpen cannot be used if EnableUpdatePanelCompatability is enabled"));
                _ClientEventOpen = value;
            }
        }

        private Control _TargetControl;
        protected Control TargetControl
        {
            get
            {
                if (_TargetControl == null)
                    _TargetControl = FindControlHelper(TargetControlID);
                return _TargetControl;
            }
        }

        protected override IEnumerable<ScriptDescriptor> GetScriptDescriptors(Control targetControl)
        {
            var element = new JQueryScriptControlDescriptor("dialog", targetControl.ClientID);
            element.AddProperty("autoOpen", AutoOpen);
            if(Height > 0)
                element.AddProperty("height", Height);
            if (Width > 0)
                element.AddProperty("width", Width);
            if(Modal)
                element.AddProperty("modal", Modal);
            if(!Draggable)
                element.AddProperty("draggable", Draggable);
            if(!string.IsNullOrEmpty(Title))
                element.AddProperty("title", Title);
            if(!Resizable)
                element.AddProperty("resizable", Resizable);
            if (!string.IsNullOrEmpty(ClientEventOpen))
                element.AddEvent("open", ClientEventOpen);
            if (!string.IsNullOrEmpty(ClientEventClose))
                element.AddEvent("close", ClientEventClose);
            if (!string.IsNullOrEmpty(Buttons))
                element.AddEvent("buttons", Buttons);

            yield return element;
        }

        protected override IEnumerable<ScriptReference> GetScriptReferences()
        {
            return null;
        }

        protected Control FindControlHelper(string id)
        {
            Control control = null;
            control = FindControl(id);
            for (Control control2 = this.NamingContainer; (control == null) && (control2 != null); control2 = control2.NamingContainer)
            {
                control = control2.FindControl(id);
            }
            return control;
        }
    }
}
